package com.ossjk.qlh.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.loginfo.LogModule;
import com.ossjk.qlh.loginfo.LogOperation;
import com.ossjk.qlh.system.entity.Permission;
import com.ossjk.qlh.system.service.IPermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-03-21
 */
@LogModule(value = "权限管理")
@Api(tags = "权限管理")
@RestController
@RequestMapping("/system/permission")
public class PermissionController extends BaseController {

	@Autowired
	private IPermissionService iPermissionService;

	@LogOperation("列表")
	@ApiOperation(value = "列表")
	@PostMapping(value = "/list")
	@RequiresPermissions("00000005")
	public ResponseBean<List<Permission>> list() {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.orderByAsc("level", "sort");
		return responseSuccess(iPermissionService.list(queryWrapper));
	}

	@LogOperation("去编辑")
	@ApiOperation(value = "去编辑")
	@PostMapping(value = "/toUpdate")
	@RequiresPermissions("000000050005")
	public ResponseBean<Permission> toUpdate(@RequestBody RequestBean requestBean) {
		Permission permission = iPermissionService.getById((String) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(permission)) {
			return responseSuccess(permission);
		} else {
			return responseFail();
		}
	}

	@LogOperation("添加")
	@ApiOperation(value = "添加")
	@PostMapping(value = "/save")
	@RequiresPermissions("000000050000")
	public ResponseBean save(@RequestBody RequestBean<Permission> requestBean) {
		Permission permission = requestBean.getData();
		// 权限查重
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("code", permission.getCode());
		if (CommonUtil.isBlank(iPermissionService.getOne(queryWrapper))) {
			if (iPermissionService.save(permission)) {
				return responseSuccess();
			} else {
				return responseFail();
			}
		} else {
			return responseFail("权限编码重复");
		}
	}

	@LogOperation("编辑")
	@ApiOperation(value = "编辑")
	@PostMapping(value = "/update")
	@RequiresPermissions("000000050005")
	public ResponseBean udate(@RequestBody RequestBean<Permission> requestBean) {
		Permission permission = requestBean.getData();
		if (iPermissionService.updateById(permission)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("删除")
	@ApiOperation(value = "删除")
	@PostMapping(value = "/remove")
	@RequiresPermissions("000000050010")
	public ResponseBean delete(@RequestBody RequestBean requestBean) {
		String id = (String) requestBean.getParam().get("id");
		// 删除子节点
		if (iPermissionService.removeChildrenById(id)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
